package com.bishe.supermarket.service;

import com.bishe.supermarket.mapper.CommodityMapper;
import com.bishe.supermarket.mapper.SaveMapper;
import com.bishe.supermarket.pojo.Commodity;
import com.bishe.supermarket.pojo.Save;
import com.bishe.supermarket.pojo.vo.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class CommodityService {

    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    SaveMapper saveMapper;
    public List<Commodity> selectAll(){

        List<Commodity> list = commodityMapper.selectAll();

        return list;
    }

    public List<Commodity> selectByName(String name){

        List<Commodity> list = commodityMapper.selectByName(name);

        return list;
    }
    /**
     *
     *
     *判断商品名称是否存在
     * 判断是否填了商品类型
     */

    public Status judge(String name,String category){

        System.out.println("service中"+name);
        System.out.println("service中"+category);
        Status status = new Status();
        List<Commodity> list = commodityMapper.selectByComName(name);
        for (Commodity c:list) {
            System.out.println("厉害"+c.getCommodityName());
        }
        if (list.isEmpty()){
           status.setCode(false);
           status.setMsg("该商品不存在！");
           return status;
        }

        if (category.equals("请选择商品类型")){
            status.setMsg("未选择商品类型");
            status.setCode(false);
            return status;
        }

       Commodity commodity = new Commodity();
        commodity.setCommodityCategory(category);
        commodity.setCommodityName(name);
        int temp = commodityMapper.update(commodity);
        System.out.println(temp);
        status.setCode(true);
        status.setMsg("修改成功！");

        return status;

    }
    //把仓库中所有商品类型传过来

    public Set<String> selectCategory(){

        List<Save> list = saveMapper.selectAll();

        List<String> stringList = new ArrayList<>();

        for (Save s:list) {
            stringList.add(s.getCommodityCategory());
        }
        Set<String> set = new HashSet<>();

        for (String s:stringList) {
            set.add(s);
        }
        return set;
    }
    //把仓库所有商品名字传过来
    public Set<String> selectcomName(){

        List<Save> list = saveMapper.selectAll();
        List<String> stringList = new ArrayList<>();
        for (Save s:list) {
            stringList.add(s.getCommodityName());
        }
        Set<String> set = new HashSet<>();

        for (String s:stringList) {
            set.add(s);
        }
        return set;
    }
    //从commodity中拿数据放在页面
    public Status getCom(String comName,String category,int comNum ){

        Status status = new Status();

        if(comNum<=0){
            status.setMsg("请输入合法数量！");
            status.setCode(false);
            return status;
        }

        //通过姓名查找，判断类型对不对，然后判断上架数量是否小于库存数量，

       List<Save> list =  saveMapper.seleceByName(comName);

     if(comName==null){
         status.setMsg("仓库暂时没货，请等待上货！");
         status.setCode(false);
         return status;
     }

        System.out.println("我是service中的list0"+list.get(0).getCommodityNum());
       if (!list.get(0).getCommodityCategory().equals(category)){
           status.setMsg("商品类型有误！重新填写！");
           status.setCode(false);
           return status;
       }
       if (list.get(0).getCommodityNum()<comNum){
           status.setMsg("库存数量不足！重新填写！");
           status.setCode(false);
           return status;
       }
       //往商品表里面插入
        //如果数量为0则从save表中删除，大于0则数量减一下
        if (list.get(0).getCommodityNum()-comNum==0){
            saveMapper.delete(comName);
        }
        if (list.get(0).getCommodityNum()-comNum>0){
            Save save = new Save();
            save.setCommodityName(comName);
            save.setCommodityNum(list.get(0).getCommodityNum()-comNum);
            saveMapper.update(save);
        }
        //如果商品表中有该商品，则只增加数量，如果没有则插入
        List<Commodity> list1 = commodityMapper.selectByComName(comName);
        if (list1.isEmpty()){
            Commodity commodity = new Commodity();
            commodity.setCommodityNum(comNum);
            commodity.setCommodityName(comName);
            commodity.setCommodityCategory(category);
            commodity.setCommodityPrice(list.get(0).getCommodityPrice());
            commodity.setCommodityPrimeCost(list.get(0).getCommodityPrimeCost());
            commodityMapper.insert(commodity);
        }else {
            //只要修改数量
            list1.get(0).setCommodityNum(list1.get(0).getCommodityNum()+comNum);
            commodityMapper.updateNum(list1.get(0));
        }

        status.setCode(true);
        status.setMsg("上架成功！");

        return status;
    }

    //对下架商品进行处理
    //1.如果货架上的数量退完了，则删除货架该记录，把该商品添加到仓库
    //2.如果没有退完，只减少数量，把该商品减去的数量添加到仓库

    public Status xiaJia(String comName,int comNum){
        Status status = new Status();
        if (comNum<=0){
            status.setMsg("请输入合法数量！");
            status.setCode(false);
            return status;
        }
      List<Commodity> list =  commodityMapper.selectByComName(comName);

        if (comNum>list.get(0).getCommodityNum()){
            status.setMsg("下架数量大于货架上商品数量！");
            status.setCode(false);
            return status;
        }
        System.out.println("我是下架商品的类型"+list.get(0).getCommodityCategory());
        if (list.get(0).getCommodityNum()-comNum==0){
            //把商品从commodity中删除，插入save
            //如果save中有，则把save中数量改了

            commodityMapper.delete(comName);
            Save save = new Save();
            save.setCommodityPrice(list.get(0).getCommodityPrice());
            save.setCommodityPrimeCost(list.get(0).getCommodityPrimeCost());
            save.setCommodityCategory(list.get(0).getCommodityCategory());
            save.setStoreId((int)(Math.random()*10)+1);
            save.setCommodityNum(comNum);
            save.setCommodityName(comName);
           List<Save> list1 = saveMapper.seleceByName(comName);
            System.out.println("是不是为空"+list1.isEmpty());
           if (!list1.isEmpty()){
               //修改数量
               Save save1 = new Save();
               save1.setCommodityName(comName);
               save1.setCommodityNum(list1.get(0).getCommodityNum()+comNum);
               saveMapper.update(save1);
           }else {
               saveMapper.insert(save);
           }


        }

        if (list.get(0).getCommodityNum()-comNum>0){
            //commodity表中减少数量，save表中增肌该记录
           Commodity commodity = new Commodity();
           commodity.setCommodityName(comName);
           commodity.setCommodityNum(list.get(0).getCommodityNum()-comNum);
            commodityMapper.updateNum(commodity);


            Save save = new Save();
            save.setCommodityPrimeCost(list.get(0).getCommodityPrimeCost());
            save.setCommodityPrice(list.get(0).getCommodityPrice());
            save.setStoreId((int)(Math.random()*10)+1);
            save.setCommodityCategory(list.get(0).getCommodityCategory());
            save.setCommodityName(comName);
            save.setCommodityNum(comNum);
            saveMapper.insert(save);

        }
        status.setCode(true);
        status.setMsg("下架成功！");
        return status;
    }

    public List<Commodity> backName(){


       List<Commodity> list = commodityMapper.selectAll();

       return list;
    }
}